#include <avr/io.h>
#define F_CPU 7372800
#include <util/delay.h>
#define BAUDRATE 9600
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)

// Define pins
#define PINCHAR1 0
#define PINCHAR2 1
#define PINCHAR3 2

	int Char1;
	int Char2;

void USART_Init(){
	UBRRL=UBRRVAL;
	UBRRH=(UBRRVAL>>8);
	UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
	//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
	UCSRB=(1<<RXEN)|(1<<TXEN);
	}

void USART_bounce(){
	uint8_t Temp;
	while(!(UCSRA&(1<<RXC))){
		PORTC = 255-(1<<PINCHAR1);
		PORTB = Char1;
		_delay_ms(0.2);
		PORTC = 255-(1<<PINCHAR2);
		PORTB = Char2;
		_delay_ms(0.2);
		};
	Temp=UDR & 0b01001111;			// Dump what we don't need
	while (!(UCSRA&(1<<UDRE))){
		PORTC = 255-(1<<PINCHAR1);
		PORTB = Char1;
		_delay_ms(0.2);
		PORTC = 255-(1<<PINCHAR2);
		PORTB = Char2;
		_delay_ms(0.2);
		};
	UDR=Temp;

	if(Temp>16){			//Select char
		PORTC = 255-(1<<PINCHAR1);
		Temp = Temp  & 0b00001111;  // Dump first four bits
		Char1 = Temp;
		}
	else{
		PORTC = 255-(1<<PINCHAR2);
		Char2 = Temp;
		}

	}

int main(void){
	DDRB = 255;				//Port D as output
	DDRC = 255;   			//Port C as output
	USART_Init();
	while(1){
		USART_bounce();
		}
	}




